﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace RemoteWakeup
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [MTAThread]
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                MessageBox.Show("RemoteWakeup.exe [target mac address] [broadcast ip] /n/r Example:00:1A:2B:3C:4D:5E 192.168.0.255");
                Application.Exit();
            }
            //MessageBox.Show(args[i]);
            try
            {
                byte[] mac = new byte[6];
                string[] mackey = args[0].Split(':');
                for (int j = 0; j < 6; j++)
                    mac[j] = Convert.ToByte(mackey[j], 16);
                string[] bipkey = args[1].Split('.');
                IPAddress bIP = new IPAddress(new byte[] { byte.Parse(bipkey[0]), byte.Parse(bipkey[1]), byte.Parse(bipkey[2]), byte.Parse(bipkey[3]) });
                wakeup(mac, bIP);
            }
            catch
            {
            }
        }
        private static void wakeup(byte[] mac, IPAddress broadcastIP)
        {
            UdpClient client = new UdpClient();
            client.Connect(broadcastIP, 30000);

            byte[] packet = new byte[17 * 6];

            for (int i = 0; i < 6; i++)
                packet[i] = 0xFF;

            for (int i = 1; i <= 16; i++)
                for (int j = 0; j < 6; j++)
                    packet[i * 6 + j] = mac[j];

            int result = client.Send(packet, packet.Length);
        }
    }
}